require('./index.css');
let _sideBar = require('../aside/index.js');
let _locationOfFileService = require('service/locationOfFileService.js');
let _util = require('util/util.js');
let UploadModal = require('util/uploadModal/index.js');

let page = {
    data: {
        uploadModal: null
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {

        _locationOfFileService
            .loadTableInfo({
                fields: {
                    "fileNo": "归档号",
                    "location": "位置",
                    "opera": "操作"
                },
                //格式化数据
                formatterData: function (data, fields) {
                    let formatterArray = [];

                    $.each(data, function (index, value) {
                        let newObj = Object.assign({}, data[index]);
                        formatterArray.push(newObj);
                    })

                    return formatterArray;
                }
            })
            .then(res => {
                _util.renderTable({
                    fields: res.fields || {},
                    formatterData: res.formatterData,
                    selector: $('#info_table'),
                    data: res.data
                });
            })

        _sideBar.init({
            name: 'file-location-manage'
        })
    },
    bindEvent: function () {

        let _this = this;

        //excel批量导入
        $(document).on('click', '.jsUploadExcel', function (e) {

            _this.data.uploadModal ? '' : (_this.data.uploadModal = new UploadModal());
            _this.data.uploadModal.render({
                container: $('.upload-wrapper')
            })
            $('.upload-wrapper').show();
        });

        //修改位置信息
        $(document).on('click', '.jsEdit', function (e) {
            let $target = $(e.currentTarget);

            $('#fileNo').val('');
            $('#location').val('');
            $('.progress .progress-bar').css('width', '0%');
            $('.save-success').css('display', 'none');

            _locationOfFileService
                .getFileLocationInfo({
                    data: {
                        fileNo: $target.data('fileposid')
                    }
                })
                .then(res => {
                    let data = res[0];
                    $('#fileNo').val(data['fileNo']);
                    $('#location').val(data['location']);
                    $('.edit-container').show();
                })
        });

        //关闭弹窗
        $('.close-btn').on('click', function (e) {
            $('.edit-container').hide();
        });

        //保存编辑信息
        $('.jsOnSave').on('click', function (e) {
            let fileNo = $('#fileNo').val(),
                location = $('#location').val();

            _locationOfFileService.saveFileLocationInfo({
                data: {
                    fileNo: fileNo,
                    location: location
                }
            })
        });


        //添加到回收站
        $(document).on('click', '.jsAddToTrash', function (e) {
            let data = $(e.currentTarget).data('fileposid');
            if (window.confirm('确定将该条数据添加到回收站？')) {
                console.log(data);
            }
        });
    }
}

$(function () {
    page.init();
})